<?php

class Goodcate extends Admincontroll{
    private $catelogic;
    public function __construct() {
        $this->catelogic=  $this->load("cate");
    }

    //后台默认首页
    public function index(){
        Temp::display();
    }
    
     public function delete(){
        $id=isset($_REQUEST["id"])?intval($_REQUEST["id"]):0;
        if(!$id){
            return $this->showmessage("参数异常！","错误信息");
        }
        $count=  $this->catelogic->get_count("parent_id={$id}");
        if($count>0){
            return $this->showmessage("改分类底下有子分类,请删除子分类后在删除该分类", "错误信息", "?Goodcate/catelist");
        }else{
            $res=$this->catelogic->delete("id={$id}");
            if($res){
                return $this->showmessage("删除成功", "成功信息", "?Goodcate/catelist");
            }
        }
    }
    //商品分类列表
    public function catelist(){
         $db= $this->catelogic->select("id,parent_id as pId,base_name as name","1=1 order by sort desc");
         Temp::assign("catelist", json_encode($db));
         Temp::display();
    }
    public function public_get_children(){
        $parent_id=isset($_REQUEST["parent_id"])?intval($_REQUEST["parent_id"]):0;
        $noid="";
        if(isset($_REQUEST["curid"])){
            $noid=" and id!={$_REQUEST["curid"]}";
        }
        $db=$this->catelogic->select("id,parent_id as pid,base_name as name"," parent_id={$parent_id} {$noid}");
        Temp::assign("list", $db);
        Temp::assign("parent_id", $parent_id);
        Temp::display();
    }
    //通过父类id添加子分类
    public function addbypid(){
         $pid= isset($_REQUEST["pid"])?$_REQUEST["pid"]:0;
         if($pid){
             $data=array();
             $this->get_parent($pid, $data);
             Temp::assign("pinfo", $data);
             Temp::display();
         }else{
             $this->add();
         }
    }
    public function edit(){
        $id=isset($_REQUEST["id"])?intval($_REQUEST["id"]):0;
        if(!$id){
            return $this->showmessage("参数异常！","错误信息");
        }
        if(isset($_REQUEST["submit"])){
            $base_name=  isset($_REQUEST["base_name"])?$_REQUEST["base_name"]:"";
            $parent_id=isset($_REQUEST["parent_id"])?$_REQUEST["parent_id"]:0;
            $desc=isset($_REQUEST["desc"])?$_REQUEST["desc"]:"";
            $sort=isset($_REQUEST["sort"])?$_REQUEST["sort"]:0;
            $parentv_id=0;
            if(is_array($parent_id)){
                foreach ($parent_id as $key=>$v){
                    if($v==-1){
                        unset($parent_id[$key]);
                    }
                }
                $parent_path=  implode(",", $parent_id);
                $parentv_id=  end($parent_id);
            }
            $goodcateinfo=array(
            "base_name"=>  Common::strEscape($base_name),
            "parent_id"=>  intval($parentv_id),
            "sort"=>$sort,
             "`desc`"=>Common::strEscape($desc),
             "create_time"=>  time(),
             "edit_time"=>  time(),
             "parent_path"=>$parent_path.","
            );
            $res=$this->catelogic->update($goodcateinfo,"id={$id}");
            if($res){
                 return $this->showmessage("更新分类成功！", "成功信息","?Goodcate/catelist");
            }else{
                 return $this->showmessage("更新分类失败！", "错误信息");
            }
            
        }else{
            $cate=  $this->catelogic->get_one("*","id={$id}");
            $data=array();
            $this->get_parent($cate["parent_id"], $data);
            Temp::assign("pinfo", $data);
            Temp::assign("cate", $cate);
        }
        Temp::assign("id", $id);
        Temp::display();
    }

        private function get_parent($pid,&$data){
        $menu=  $this->catelogic->select("*","id={$pid}");
        $tmp=array();
        if(!$menu){
            $data[]=0;
            return ;
        }
        if($menu[0]["parent_id"]!=0){
            $this->get_parent($menu[0]["parent_id"], $data);
            $menut=  $this->catelogic->select("*","parent_id={$menu[0]["parent_id"]}");
            $tmp["selected"]=$pid;
            $tmp["data"]=$menut;
            $data[]=$tmp;
           
        }else{
            $menu=  $this->catelogic->select("*","parent_id=0");
            $tmp["selected"]=$pid;
            $tmp["data"]=$menu;
            $data[]=$tmp;
            
        }
    }
    public function add(){
        if(isset($_REQUEST["submit"])){
            $base_name=  isset($_REQUEST["base_name"])?$_REQUEST["base_name"]:"";
            $parent_id=isset($_REQUEST["parent_id"])?$_REQUEST["parent_id"]:0;
            $desc=isset($_REQUEST["desc"])?$_REQUEST["desc"]:"";
            $sort=isset($_REQUEST["sort"])?$_REQUEST["sort"]:0;
            $parentv_id=0;
            if(is_array($parent_id)){
                foreach ($parent_id as $key=>$v){
                    if($v==-1){
                        unset($parent_id[$key]);
                    }
                }
                $parent_path=  implode(",", $parent_id);
                $parentv_id=  end($parent_id);
            }
            $goodcateinfo=array(
            "base_name"=>  Common::strEscape($base_name),
            "parent_id"=>  intval($parentv_id),
            "sort"=>$sort,
             "`desc`"=>Common::strEscape($desc),
             "create_time"=>  time(),
             "edit_time"=>  time(),
             "parent_path"=>$parent_path.","
            );
            $res=$this->catelogic->insert($goodcateinfo);
            if($res){
                 return $this->showmessage("添加分类成功！", "成功信息","?Goodcate/catelist");
            }else{
                 return $this->showmessage("添加分类失败！", "错误信息");
            }
            
        }else{
           $db=$this->catelogic->select("id,parent_id as pid,base_name as name"," parent_id=0");
           Temp::assign("list", $db);
           Temp::display(); 
        }
        
    }
}
